﻿using SwanEngine.Astar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace AstarBuilder
{
    [CanEditMultipleObjects()]
    [CustomEditor(typeof(MapGridBuilder), true)]
    /// <summary>
    /// 生成A星格子用
    /// </summary>
    public class AstarGridBuilder : Editor
    {
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            MapGridBuilder mapBuilder = target as MapGridBuilder;

            if (GUILayout.Button("烘焙场景", GUILayout.Height(30)))
            {
                if (CheckSceneID(mapBuilder))
                {
                    mapBuilder.BakeGrid();
                }
            }

            if (GUILayout.Button("显示网格", GUILayout.Height(30)))
            {
                do
                {
                    if (mapBuilder.nodeDic.Count == 0)
                    {
                        if (!CheckSceneID(mapBuilder))
                        {
                            break;
                        }
                        if (!File.Exists(mapBuilder.GetAStarFilePath()))
                        {
                            Debug.LogError("请先烘培格子数据");
                            break;
                        }
                        if (!mapBuilder.LoadAstarFile())
                        {
                            Debug.LogError("加载格子文件异常 path:" + mapBuilder.GetAStarFilePath());
                            break;
                        }
                    }
                    mapBuilder.ShowGrid();
                } while (false);
            }

            if (GUILayout.Button("隐藏网格", GUILayout.Height(30)))
            {
                mapBuilder.HideGrid();
            }
            if (GUILayout.Button("初始化A*", GUILayout.Height(30)))
            {
                mapBuilder.InitAstart();
            }
            if (GUILayout.Button("寻路", GUILayout.Height(30)))
            {
                mapBuilder.FindPath();
            }
        }

        public bool CheckSceneID(MapGridBuilder builder)
        {
            if (string.IsNullOrEmpty(builder.sceneId))
            {
                Debug.LogError("找不到A*文件路径 SceneId为null");

                return false;
            }
            return true;
        }
    }
}
